﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DrugStorehouseMIS.DAL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.BLL
{
    public class StockInDetail
    {
        #region 封装属性

        private string mStockInNum;

        public string StockInNum
        {
            get { return mStockInNum; }
            set { mStockInNum = value; }
        }

        private Drug mDrugNum;

        public Drug DrugNum
        {
            get { return mDrugNum; }
            set { mDrugNum = value; }
        }

        private string mProductedArea;

        public string ProductedArea
        {
            get { return mProductedArea; }
            set { mProductedArea = value; }
        }

        private DateTime mProductedDatetime; //生产日期

        public DateTime ProductedDatetime
        {
            get { return mProductedDatetime; }
            set { mProductedDatetime = value; }
        }

        private DateTime mExpiryDatetime; //有效日期

        public DateTime ExpiryDatetime
        {
            get { return mExpiryDatetime; }
            set { mExpiryDatetime = value; }
        }

        private float mImportPrice;   //进货价

        public float ImportPrice
        {
            get { return mImportPrice; }
            set { mImportPrice = value; }
        }

        private string mBatchNum;   //批号

        public string BatchNum
        {
            get { return mBatchNum; }
            set { mBatchNum = value; }
        }

        private int mQuantity; //入库数量

        public int Quantity
        {
            get { return mQuantity; }
            set { mQuantity = value; }
        }

        private float mAmount;  //金额

        public float Amount
        {
            get { return mAmount; }
            set { mAmount = value; }
        }

        #endregion

        private IStockInDetailDAL dal;

        public StockInDetail()
        {
            dal = Adapter.GetStockInDetail();
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="dr"></param>
        public void FetchObject(IDataReader dr)
        {
            if (dr != null)
            {
                mStockInNum = ConvertDataType.CheckFiledString(dr["stock_in_num"]);
                mDrugNum.DrugNum = ConvertDataType.CheckFiledInt(dr["drug_num"]);
                mAmount = ConvertDataType.CheckFieldFloat(dr["amount"]);
                mBatchNum = ConvertDataType.CheckFiledString(dr["batch_num"]);
                mExpiryDatetime = ConvertDataType.CheckFiledDateTime(dr["expiry_datetime"]);
                mImportPrice = ConvertDataType.CheckFieldFloat(dr["import_price"]);                
                mProductedArea = ConvertDataType.CheckFiledString(dr["producted_area"]);
                mProductedDatetime = ConvertDataType.CheckFiledDateTime(dr["producted_datetime"]);
                mQuantity = ConvertDataType.CheckFiledInt(dr["quantity"]);                                         
            }
        }

        /// <summary>
        ///  根据单号获取
        /// </summary>
        /// <param name="StockInNum"></param>
        /// <returns></returns>
        public StockInDetail GetStockInDetail(string StockInNum)
        {
            StockInDetail obj = new StockInDetail();
            obj.DrugNum= new Drug();
            IDataReader dr = dal.SelectByNum(StockInNum);
            while (dr != null && dr.Read())
            {
                obj.FetchObject(dr);
            }
            return obj;
        }

        /// <summary>
        /// 获取同一单号的所有对象
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public IList<StockInDetail> GetStockInDetailAll(string num)
        {
            IList<StockInDetail> list = new List<StockInDetail>();
            IDataReader dr = dal.SelectByNum(num);
            while (dr != null && dr.Read())
            {
                StockInDetail obj = new StockInDetail();
                obj.DrugNum = new Drug();

                obj.FetchObject(dr);
                list.Add(obj);
            }
            return list;
        }

        /// <summary>
        /// 根据单号获取入库明细单，返回datatabel
        /// </summary>
        /// <param name="stockInNum"></param>
        /// <returns></returns>
        public DataTable GetStockInDeatilByNum(string stockInNum)
        {
            return dal.Select(stockInNum);
        }

        /// <summary>
        /// 创建入库明细单
        /// </summary>
        /// <param name="stockInDetail"></param>
        /// <returns></returns>
        public bool CreateStockInDetail(StockInDetail stockInDetail)
        {
            return dal.Insert(stockInDetail.StockInNum, stockInDetail.DrugNum.DrugNum, stockInDetail.ProductedArea, stockInDetail.ProductedDatetime, stockInDetail.ExpiryDatetime, stockInDetail.ImportPrice, stockInDetail.BatchNum,stockInDetail.Quantity, stockInDetail.Amount) > 0 ? true : false;
        }

        /// <summary>
        /// 删除一条明细记录
        /// </summary>
        /// <param name="stockInNum"></param>
        /// <param name="drugNum"></param>
        /// <returns></returns>
        public bool RemoveStockInDetail(string stockInNum, int drugNum)
        {
            return dal.Delete(stockInNum, drugNum) > 0 ? true : false;
        }

        /// <summary>
        ///  删除同一单号的所有记录
        /// </summary>
        /// <param name="stockInNum"></param>
        /// <returns></returns>
        public bool RemoveStockInDetail(string stockInNum)
        {
            return dal.Delete(stockInNum) > 0 ? true : false;
        }

        /// <summary>
        /// 更新入库单
        /// </summary>
        /// <param name="stockInDetail"></param>
        /// <returns></returns>
        public bool SetStockInDetail(StockInDetail stockInDetail)
        {
            return dal.Update(stockInDetail.StockInNum, stockInDetail.DrugNum.DrugNum, stockInDetail.ProductedArea, stockInDetail.ProductedDatetime, stockInDetail.ExpiryDatetime, stockInDetail.ImportPrice, stockInDetail.BatchNum, stockInDetail.Quantity, stockInDetail.Amount) > 0 ? true : false;
        }


    }
}
